package cn.afactory.tygz.ui.base.views.search;

import java.lang.ref.WeakReference;
import java.util.EnumMap;
import java.util.List;

import cn.afactory.tygz.ui.base.entity.LoganHotWordEntity;

/**
 * Created by Kenneth on 15/9/13.
 */
public class SearchHotKeyWordCache {
    private static SearchHotKeyWordCache mInstance;
    private EnumMap<SearchSourcePager, WeakReference<List<LoganHotWordEntity>>> mCache
            = new EnumMap<SearchSourcePager, WeakReference<List<LoganHotWordEntity>>>(SearchSourcePager.class);

    private SearchHotKeyWordCache() {
    }

    public static SearchHotKeyWordCache getInstance() {
        if (mInstance == null) {
            mInstance = new SearchHotKeyWordCache();
        }
        return mInstance;
    }

    public void putCache(SearchSourcePager searchSourcePager, List<LoganHotWordEntity> hwList) {
        if (!hwList.isEmpty()) {
            WeakReference<List<LoganHotWordEntity>> cacheList = new WeakReference<List<LoganHotWordEntity>>(hwList);
            mCache.put(searchSourcePager, cacheList);
        }
    }

    public List<LoganHotWordEntity> getCacheHotKeyWord(SearchSourcePager searchSourcePager) {
        WeakReference<List<LoganHotWordEntity>> cacheList = mCache.get(searchSourcePager);
        if (cacheList == null) {
            cacheList = mCache.get(SearchSourcePager.COMPANY);
        }
        return cacheList != null ? cacheList.get() : null;
    }
}
